/**
 * 
 */
package com.uuticketweb.ticket.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import org.apache.commons.lang.StringUtils;

/**
 * @author stephen
 *
 */
public class DateHelper {
	private static SimpleDateFormat DB_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat DB_DATETIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static SimpleDateFormat CMD_DATETIME_FORMAT = new SimpleDateFormat("dMMM", Locale.ENGLISH);
	private static SimpleDateFormat NONE_SYBLE_FORMAT = new SimpleDateFormat("yyyyMMdd");

	private DateHelper() {
	}

	public static String toDBString(Calendar date) {
		return DB_DATE_FORMAT.format(date.getTime());
	}

	public static String toDateTimeString(Calendar date) {
		return DB_DATETIME_FORMAT.format(date.getTime());
	}

	public static Date parseDate(String date) throws ParseException {
		return DB_DATE_FORMAT.parse(date);
	}

	public static int getPeriod(String date1, String date2) throws ParseException {
		int betweenDays = 0;
		Date d1 = DB_DATE_FORMAT.parse(date1);
		Date d2 = DB_DATE_FORMAT.parse(date2);
		betweenDays = (int)((d2.getTime() - d1.getTime())/1000/60/60/24);
		return Math.abs(betweenDays);
	}

	public static int getWeekDay(Calendar date) {
		int i = date.get(Calendar.DAY_OF_WEEK) -1;
		i = (i == 0? 7 : i);
		return i;
	}

	public static void main(String[] args) {
		try {
			/*System.out.println("2009-1-2 is " + getDay("2009-1-2"));
			System.out.println("2009-1-3 is " + getDay("2009-1-3"));
			System.out.println("2009-1-4 is " + getDay("2009-1-4"));
			System.out.println("2009-1-5 is " + getDay("2009-1-5"));
			System.out.println("2009-1-6 is " + getDay("2009-1-6"));*/
			
			String strDate = "2009-9-28";
			//Date date = DB_DATE_FORMAT.parse(strDate);
			System.out.println(strDate + " = " + isDateCrossSeasonOrYear(strDate));
			System.out.println(StringUtils.leftPad("106", 6, "0"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static String toCMDDate(String date) throws ParseException {
		Date time = DB_DATE_FORMAT.parse(date);
		return CMD_DATETIME_FORMAT.format(time);
	}
	
	public static String toCMDDate(Date date) throws ParseException {
		return CMD_DATETIME_FORMAT.format(date);
	}

	public static String noneSybleParse(String date) throws ParseException {
		Date time = NONE_SYBLE_FORMAT.parse(date);
		return DB_DATE_FORMAT.format(time);
	}
	
	/**
	 * 根据日期得到季度
	 * @param date
	 * @return
	 */
	public static String getSeason(String strDate){
		String season = "";
		int month = 0;
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(DB_DATE_FORMAT.parse(strDate));
			month = cal.get(Calendar.MONTH);
			if(month >= 0){
				if(month >=0 && month <=2){
					season = "1";
				}else if(month >=3 && month <=5){
					season = "2";
				}else if(month >=6 && month <=8){
					season = "3";
				}else if(month >=9 && month <=11){
					season = "4";
				}
			}
		} catch (ParseException e) {
			e.printStackTrace();
			
		}
		return season;
	}
	
	/**
	 * 根据日期得到星期几
	 * @param date
	 * @return 1 if Monday, 7 if Sunday. Different with the Calendar value
	 */
	public static String getDay(String strDate){
		String day = "";
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(DB_DATE_FORMAT.parse(strDate));
			int iday = cal.get(Calendar.DAY_OF_WEEK) + 6;
			iday = iday - 7 == 0 ? iday : iday - 7;
			day = String.valueOf(iday);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return day;
	}
	
	/**
	 * 根据日期得到年份
	 * @param date
	 * @return
	 */
	public static String getYear(String date){
		return date.substring(0, 4);
	}
	
	/**
	 * Add some days to a date
	 * @param date
	 * @param dateAmount
	 * @return
	 */
	public static Date addDays(Date date, int dateAmount) {
		if(dateAmount == 0) {
            return date;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, dateAmount);
        return calendar.getTime();
	}
	
	/**
	 * Parse the date to see that 2 days after the day will be in 
	 * different season or different year with the original date.
	 * @param date Original date
	 * @return 1 cross year, 2 cross season , 0 nothing
	 */
	public static int isDateCrossSeasonOrYear(String strDate) throws Exception {
		Date date = parseDate(strDate);
		Date date2 = addDays(date, 2);
		Calendar calendar1 = Calendar.getInstance();
		calendar1.setTime(date);
		Calendar calendar2 = Calendar.getInstance();
		calendar2.setTime(date2);
		if(calendar2.get(Calendar.YEAR) - calendar1.get(Calendar.YEAR) == 1) {
			return 1;
		}
		int month1 = calendar1.get(Calendar.MONTH);
		if(month1 % 3 == 2 && calendar2.get(Calendar.MONTH) - month1 == 1) {
			return 2;
		}
		return 0;
	}
}
